package eu.szemraj.managers {
    import eu.szemraj.events.ContextMenuManagerEvent;    
    
    import flash.ui.ContextMenuBuiltInItems;    
    import flash.ui.ContextMenuItem;    
    import flash.events.ContextMenuEvent;    
    import flash.ui.ContextMenu;    
    import flash.display.InteractiveObject;    

    import eu.szemraj.events.DisposableEventDispatcher;    

    public class ContextMenuManager extends DisposableEventDispatcher {

        protected var menu:ContextMenu;        protected var menuContext:InteractiveObject;

        public function ContextMenuManager( menuContext:InteractiveObject, hideBuildInItems:Boolean = true ) {
        	
            this.menuContext = menuContext;
            menu = new ContextMenu( );
            if( hideBuildInItems ) 
            	menu.hideBuiltInItems( );
            this.menuContext.contextMenu = menu;
            menu.addEventListener( ContextMenuEvent.MENU_SELECT, dispatchContextMenuEvent );
        }

        public function addItem( caption:String, handler:Function = null, separatorBefore:Boolean = false, enabled:Boolean = true, visible:Boolean = true ):ContextMenuItem {
        	
            var cmItem:ContextMenuItem = createItem( caption, handler, separatorBefore, enabled, visible );
            menu.customItems.push( cmItem );
            return cmItem;        	
        }
        
        public function removeItem(id:*):void
		{
			if(id is String)
			{
				id = getIndexByCaption(id);
			}
			customItems.splice(id as Number,1);
		}

        public function get contextMenu():ContextMenu {
        	
            return menu;
        }

        public function get builtInItems():ContextMenuBuiltInItems {
        	
            return menu.builtInItems;
        }

        public function get customItems():Array {
        	
            return menu.customItems;
        }

        public function getItem(id:*):ContextMenuItem {
        	
            if(id is String) {
                id = getIndexByCaption( id );
            }
            return menu.customItems[id];
        }

        public function hideBuiltInItems():void {
        	
            menu.hideBuiltInItems( );
        }

        protected function createItem(caption:String, handler:Function = null, separatorBefore:Boolean = false, enabled:Boolean = true, visible:Boolean = true):ContextMenuItem {
            var result:ContextMenuItem = new ContextMenuItem( caption, separatorBefore, enabled, visible );
			
            if( handler != null ) {
         	
                result.addEventListener( ContextMenuEvent.MENU_ITEM_SELECT, handler );
            }
            result.addEventListener( ContextMenuEvent.MENU_ITEM_SELECT, dispatchContextMenuEvent );
            return result;
        }

        protected function getIndexByCaption(caption:String):int {
            for ( var i:uint = 0 ; i < menu.customItems.length ; ++i ) {
                if(menu.customItems[i].caption == caption) {
                    return i;
                }
            }
            return -1;
        }
        
        private function dispatchContextMenuEvent(event:ContextMenuEvent):void {
        	
        	var contextMenuItem:ContextMenuItem = event.type == ContextMenuEvent.MENU_ITEM_SELECT ? event.target as ContextMenuItem : null;
            var contextMenuManagerEvent:ContextMenuManagerEvent = new ContextMenuManagerEvent( event, contextMenuItem );
            dispatchEvent( contextMenuManagerEvent );
        }
    }
}
